namespace TomatoBreak.UnitSpecs.Routing
{
    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Linq;
    using System.Web.Routing;

    public class RouteMatchingResult
    {
        public RouteMatchingResult(RouteData routeData)
        {
            Controller = routeData.GetRequiredString("controller");
            Action = routeData.GetRequiredString("action");
            Values = new ValuesDictionary(routeData.Values.ToDictionary(d => d.Key, d => d.Value, StringComparer.OrdinalIgnoreCase));
        }

        public string Controller { get; private set; }

        public string Action { get; private set; }

        public dynamic Values { get; private set; }

        private sealed class ValuesDictionary : DynamicObject
        {
            private readonly IDictionary<string, object> dictinary;

            public ValuesDictionary(IDictionary<string, object> dictinary)
            {
                this.dictinary = dictinary;
            }

            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                return dictinary.TryGetValue(binder.Name, out result);
            }

            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                dictinary[binder.Name] = value;
                return true;
            }
        }
    }
}